package com.xunge.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xunge.mapper.GradeMapper;
import com.xunge.pojo.Grade;
import com.xunge.pojo.GradeQueryParam;
import com.xunge.pojo.PageResult;
import com.xunge.service.GradeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class GradeServiceImpl implements GradeService {

    @Autowired
    private GradeMapper gradeMapper;

    /**
     * 分页查询成绩信息
     */
    @Override
    public PageResult<Grade> page(GradeQueryParam gradeQueryParam) {
        // 设置分页参数
        PageHelper.startPage(gradeQueryParam.getPage(), gradeQueryParam.getPageSize());
        // 执行查询
        Page<Grade> page = (Page<Grade>) gradeMapper.list(gradeQueryParam);
        // 封装分页结果
        return new PageResult<>(page.getTotal(), page.getResult());
    }

    /**
     * 新增成绩
     */
    @Override
    public void save(Grade grade) {
        grade.setCreateTime(LocalDateTime.now());
        grade.setUpdateTime(LocalDateTime.now());
        gradeMapper.insert(grade);
    }

    /**
     * 根据ID查询成绩
     */
    @Override
    public Grade getById(Integer id) {
        return gradeMapper.getById(id);
    }

    /**
     * 根据ID修改成绩
     */
    @Override
    public void update(Grade grade) {
        grade.setUpdateTime(LocalDateTime.now());
        gradeMapper.update(grade);
    }

    /**
     * 根据ID删除成绩
     */
    @Override
    public void deleteById(Integer id) {
        gradeMapper.deleteById(id);
    }

    /**
     * 批量删除成绩
     */
    @Override
    public void delete(List<Integer> ids) {
        gradeMapper.deleteByIds(ids);
    }

    /**
     * 根据学生ID查询成绩
     */
    @Override
    public List<Grade> getByStudentId(Integer studentId) {
        return gradeMapper.getByStudentId(studentId);
    }

    /**
     * 根据课程ID查询成绩
     */
    @Override
    public List<Grade> getByCourseId(Integer courseId) {
        return gradeMapper.getByCourseId(courseId);
    }

    /**
     * 批量导入成绩
     */
    @Override
    public void batchImport(List<Grade> grades) {
        for (Grade grade : grades) {
            grade.setCreateTime(LocalDateTime.now());
            grade.setUpdateTime(LocalDateTime.now());
            gradeMapper.insert(grade);
        }
    }
} 